//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package org_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/org_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type DepartMentInfoDbItem struct {
	DepartMentId       string `bson:"_id"`
	OrgId              string `bson:"orgId"`
	DepartMentName     string `bson:"departMentName"`
	ParentDepartMentId string `bson:"parentDepartMentId"`
	CreateTime         int64  `bson:"createTime"`
	CreateUserId       string `bson:"createUserId"`
	UpdateTime         int64  `bson:"updateTime"`
	SubDepartMentCount uint32 `bson:"subDepartMentCount"`
	SubMemberCount     uint32 `bson:"subMemberCount"`
	AssetCount         uint32 `bson:"assetCount"`    //只记录部门公共资产数量
	EvaluateCount      uint32 `bson:"evaluateCount"` //部门关联评估数量
}

func (item *DepartMentInfoDbItem) ToDepartMentInfo() *org_api.DepartMentInfo {
	return &org_api.DepartMentInfo{
		DepartMentId:       item.DepartMentId,
		DepartMentName:     item.DepartMentName,
		ParentDepartMentId: item.ParentDepartMentId,
		CreateTime:         item.CreateTime,
		CreateUserId:       item.CreateUserId,
		UpdateTime:         item.UpdateTime,
		SubDepartMentCount: item.SubDepartMentCount,
		SubMemberCount:     item.SubMemberCount,
		AssetCount:         item.AssetCount,
		EvaluateCount:      item.EvaluateCount,
	}
}

type _DepartMentDao struct {
	mc *mongo.Collection
}

func newDepartMentDao(db *mongo.Database) (*_DepartMentDao, error) {
	mc := db.Collection(db_ns.DB_NS_ORG + "department")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"orgId", 1}, {"parentDepartMentId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_DepartMentDao{mc: mc}, nil
}

func (db *_DepartMentDao) Insert(ctx context.Context, dbItem *DepartMentInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_DepartMentDao) Exist(ctx context.Context, orgId, departMentId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": departMentId, "orgId": orgId})
	return count > 0, err
}

func (db *_DepartMentDao) Get(ctx context.Context, orgId, departMentId string) (*DepartMentInfoDbItem, error) {
	retItem := &DepartMentInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": departMentId, "orgId": orgId}).Decode(retItem)
	return retItem, err
}

func (db *_DepartMentDao) Remove(ctx context.Context, orgId, departMentId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": departMentId, "orgId": orgId})
	return err
}

func (db *_DepartMentDao) RemoveByOrg(ctx context.Context, orgId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"orgId": orgId})
	return err
}

func (db *_DepartMentDao) UpdateName(ctx context.Context, orgId, departMentId, departMentName string, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": departMentId, "orgId": orgId}, bson.M{"$set": bson.M{"departMentName": departMentName, "updateTime": updateTime}})
	return err
}

func (db *_DepartMentDao) UpdateParent(ctx context.Context, orgId, departMentId, parentDepartMentId string, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": departMentId, "orgId": orgId}, bson.M{"$set": bson.M{"parentDepartMentId": parentDepartMentId, "updateTime": updateTime}})
	return err
}

func (db *_DepartMentDao) UpdateSubDepartMentCount(ctx context.Context, orgId, departMentId string, count uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": departMentId, "orgId": orgId}, bson.M{"$set": bson.M{"subDepartMentCount": count}})
	return err
}

func (db *_DepartMentDao) UpdateSubMemberCount(ctx context.Context, orgId, departMentId string, count uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": departMentId, "orgId": orgId}, bson.M{"$set": bson.M{"subMemberCount": count}})
	return err
}

func (db *_DepartMentDao) UpdateAssetCount(ctx context.Context, orgId, departMentId string, count uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": departMentId, "orgId": orgId}, bson.M{"$set": bson.M{"assetCount": count}})
	return err
}

func (db *_DepartMentDao) UpdateEvaluateCount(ctx context.Context, orgId, departMentId string, count uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": departMentId, "orgId": orgId}, bson.M{"$set": bson.M{"evaluateCount": count}})
	return err
}

func (db *_DepartMentDao) CountSubDepartMent(ctx context.Context, orgId, parentDepartMentId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"orgId": orgId, "parentDepartMentId": parentDepartMentId})
	return uint32(count), err
}

func (db *_DepartMentDao) CountByOrg(ctx context.Context, orgId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"orgId": orgId})
	return uint32(count), err
}

func (db *_DepartMentDao) ListByOrg(ctx context.Context, orgId string) ([]*DepartMentInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"orgId": orgId}, options.Find().SetSort(bson.M{"createTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*DepartMentInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}
